package com.taotao.tools.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


public class JacksonTester {
    public static void main(String args[]) {
        //map json to student
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            String jsonString = "[{\"name\":\"Mahesh\", \"agea\":21, \"id\":\"\"}]";
            List<Student> student = mapper.readValue(jsonString, new TypeReference<List<Student>>() {
            });

            //另一种写法
//			List<Student> student = mapper.readValue(jsonString, mapper.getTypeFactory().constructParametricType(List.class, Student.class));
            System.out.println(student);
            System.err.println(student.get(0).getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.err.println("-------------------------------");

        //map student to json
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
            TimeZone china = TimeZone.getTimeZone("GMT+08:00");
            mapper.setTimeZone(china);
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

            Student student = new Student("haha", null, "", new Date());
            System.out.println(student);
            List<Student> list = new ArrayList<>();
            list.add(student);
            String jsonString = mapper.writeValueAsString(list);
            System.out.println(jsonString);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


//@JsonIgnoreProperties(ignoreUnknown = true)
class Student {
    private String name;
    private Long age;
    private String id;
    private Date time;

    Student() {
    }

    public Student(String name, Long age, String id, Date time) {
        this.name = name;
        this.age = age;
        this.id = id;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAge() {
        return age;
    }

    public void setAge(Long age) {
        this.age = age;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id='" + id + '\'' +
                ", time=" + time +
                '}';
    }
}